import java.util.Arrays;

public class TestHeap {
    public int[] elem;
    public int usedSize;

    public TestHeap() {
        this.elem = new int[10];
    }

    public void initElem(int[] array) {
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }
    }

    public void createHeap() {
        for (int parent = (this.usedSize-1-1)/2; parent >= 0 ; parent--) {
            siftDown(parent,this.usedSize);
        }
    }

    public  void siftDown(int parent,int useSize){
        int child = parent*2+1;
        while(child<useSize) {
            if (child+1<useSize&&elem[child] < elem[child + 1]){
                child=child+1;
            }

            if (elem[child] > elem[parent]) {
                    int tem = elem[child];
                    elem[child] = elem[parent];
                    elem[parent] = tem;
                    parent=child;
                    child=parent*2+1;
            }
            else{
                break;
            }
        }
    }

    public void push(int val){
         if(isFull()){
             elem= Arrays.copyOf(elem,2*elem.length);
         }
         elem[usedSize]=val;
         siftUp(usedSize);
         usedSize++;
    }

    private void siftUp(int usedSize) {
        int child = usedSize;
        int prev = (child-1)/2;
        while((elem[prev]<elem[child])&&prev>=0){
            if (elem[child]>elem[prev]){
                int tem = elem[prev];
                elem[prev] = elem[child];
                elem[child] = tem;
                child = prev;
                prev = (child-1)/2;
            }
        }
    }

    private boolean isFull() {
        return usedSize==elem.length;
    }
}
